package com.ftg.learn.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ftg.learn.mall.entity.PmsSkuStock;
import com.ftg.learn.mall.service.IPmsSkuStockService;
import com.ftg.learn.util.ResEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * sku的库存 前端控制器
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/mall/pmsSkuStock")
public class PmsSkuStockController {

    @Autowired
    IPmsSkuStockService iPmsSkuStockService;


    /**
     * 根据商品id及sku编码模糊搜索sku库存
     * 还有批量更新sku库存信息
     * @param keyword
     * @param productId
     * @return
     */
    @GetMapping("/findSku")
    public ResEntity findSku(@RequestParam(value = "keyword", required = false) String keyword, @RequestParam Long productId) {
        if (!StringUtils.isBlank(keyword)) {
            QueryWrapper<PmsSkuStock> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_id", productId).like("sku_code", keyword);
            return new ResEntity(0, "success", iPmsSkuStockService.list(queryWrapper));
        }
        QueryWrapper<PmsSkuStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        return new ResEntity(0, "success", iPmsSkuStockService.list(queryWrapper));
    }

    /**
     *  批量更新sku库存信息
     * @param skuStockList
     * @return
     */
    @PutMapping("/updateSku")
    public ResEntity updateSku(@RequestBody List<PmsSkuStock> skuStockList){
        return iPmsSkuStockService.updateSku(skuStockList) ? new ResEntity() : new ResEntity(-1,"failure");
    }



}
